package ChessLike::BoardUtils;
use strict;
use warnings;

=head1 NAME

ChessLike::BoardUtils - The great new ChessLike::BoardUtils!

=head1 VERSION

Version 0.0.1

=cut

use version; our $VERSION = qv("v0.0.1");
use ChessLike::Board;

=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    use ChessLike::BoardUtils;

    my $foo = ChessLike::BoardUtils->new();
    ...

=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=cut

use base 'Exporter';
our @EXPORT = qw( find_tile_contents
                  next_tile_contents
                );
our %EXPORT_TAGS = ( 'all' => \@EXPORT );

=head1 API

=head2 find_tile_contents( $chesslike_board_ref )

 Purpose: Initializes a subroutine that iterates through all tile contents in
          the supplied instance of ChessLike::Board.
 Returns: $iterator
 Throws: Dies if $chesslike_board_ref is not an instance of ChessLike::Board

=cut

sub find_tile_contents {
   my $board = shift;
   die "$board is not an instance of ChessLike::Board"
      if ( ref $board ne 'ChessLike::Board' );

   my $n_tiles = $board->x_tiles() * $board->y_tiles();
   my $tile  = 0;  # Current tile
   my $index = 0;  # Current index
   my $contents_ref;  # Cache of gamepieces for current tile

   return sub {
      my @requested_values = @_;
      my @occupied_location;

      # Find the tile and index of next game piece
      while ( @occupied_location == 0 ) {
         # Check if iterator is exhausted
         return if ( $tile == $n_tiles );

         if ( $index == 0 ) {
            $contents_ref = $board->get_tile_contents( $tile, 'gamepieces' );
         }

         if ( defined $contents_ref->[$index] ) {
            @occupied_location = ( $tile, $index );
            $index++;
         }
         else {
            $tile++;
            $index = 0;
         }
      }

      # Return contents prepended with tile and index!
      return $board->convert( $occupied_location[0], 'tile' ),
             $occupied_location[1],
             $board->get_tile_contents_for_index(
                @occupied_location, @requested_values );
   }
}

=head2 next_tile_contents( $iterator, @options )

 Purpose: Returns tile, index, and requested values for next tile contents for
          $iterator.  Accepts `gamepiece', `team', `direction', and `graphic'.
 Returns: ( $tile, $index, @requested_values ) or () if iterator is exhausted.
 Throws: none

=cut

sub next_tile_contents {
   my $iterator = shift;
   return $iterator->( @_ );
}

=head1 AUTHOR

Michael J. Lewis, C<< <mjlewis at ualberta.net> >>

=head1 ACKNOWLEDGEMENTS

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Michael J. Lewis.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of ChessLike::BoardUtils
